package net.lainiao.zhihu.control.front;

import net.lainiao.zhihu.common.MyPage;
import net.lainiao.zhihu.model.Qutu;
import net.lainiao.zhihu.model.Qutupage;
import net.lainiao.zhihu.model.Qutupagegif;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import net.lainiao.zhihu.service.QutuService;
import net.lainiao.zhihu.service.QutupageService;
import net.lainiao.zhihu.service.QutupagegifService;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2016/1/18.
 */
@Controller
@RequestMapping(value="/front/qutu")
public class QutuControl {
    @Resource
    private QutuService qutuService;
    @Resource
    private QutupageService qutupageService;
    @Resource
    private QutupagegifService qutupagegifService;

    @RequestMapping(method= RequestMethod.GET)
    public String index(ModelMap modelMap){
        List<Qutu> list= qutuService.getAll();
        modelMap.put("list",list);
        return "front/qutu";
    }
    @RequestMapping(value="/page/{id}")
    public String page(@PathVariable("id") String id,ModelMap modelMap){
        return getqutuPage(id,1,modelMap);
    }

    @RequestMapping(value="/page/{id}/{pageindex}")
    public String pageindex(@PathVariable("id") String id,@PathVariable("pageindex") String pageindex,ModelMap modelMap){
        return getqutuPage(id,Integer.parseInt(pageindex),modelMap);
    }

    public String getqutuPage(String id,int pageindex,ModelMap modelMap){
        MyPage myPage=new MyPage();
        myPage.setPageSize(20);
        myPage.setCurrentPage(pageindex);
        myPage.setObject(id);
        List<Qutupage> list=qutupageService.getListPage(myPage);
        modelMap.put("list",list);
        modelMap.put("total", myPage.getTotalPage());
        int start=pageindex-5;
        if(start<1){
            start=1;
        }
        int end=pageindex+5;
        if(end> myPage.getTotalPage()){
            end= myPage.getTotalPage();
        }
        modelMap.put("pageid",id);
        modelMap.put("start",start);
        modelMap.put("end",end);
        return "front/qutupage";
    }

    @RequestMapping(value="/pagegif/{pageid}")
    public String pagegif(ModelMap modelMap,@PathVariable("pageid") String pageid){
        List<Qutupagegif> list= qutupagegifService.getListPageId(pageid);
        modelMap.put("list",list);
        return "front/qutupagegif";
    }
}
